/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.query.IsfdModelManagementNecessaryItemsQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementNecessaryItemsVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementStructureDefinitionTreeNodeVo;
import com.comac.ins.isfd.service.IIsfdModelManagementNecessaryItemsService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 模型管理-必备件库controller
 *
 * @author Lion Li
 * @date 2024-08-19
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/modelManagementNecessaryItems")
public class IsfdModelManagementNecessaryItemsController extends BaseController {

    @Autowired
    private IIsfdModelManagementNecessaryItemsService isfdModelManagementNecessaryItemsService;

    /**
     * 必备件关联的模型管理左侧树查询
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:tree")
    @GetMapping("/listTree")
    public R<List<IsfdModelManagementStructureDefinitionTreeNodeVo>> listTree() {
        return R.ok(isfdModelManagementNecessaryItemsService.listTree());
    }

    /**
     * 根据部段查询组件列表
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:tree")
    @GetMapping("/getComponentList")
    public R<List<IsfdModelManagementStructureDefinitionTreeNodeVo>> getComponentList(@RequestParam("modelNumber") String modelNumber,
                                                                                      @RequestParam("batchNumber") String batchNumber,
                                                                                      @RequestParam("sectionNumber") String sectionNumber,
                                                                                      @RequestParam(value = "needCount", required = false) Boolean needCount) {
        return R.ok(isfdModelManagementNecessaryItemsService.getComponentList(modelNumber, batchNumber, sectionNumber, needCount));
    }

    /**
     * 必备件库分页列表查询
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdModelManagementNecessaryItemsVo> list(@RequestBody IsfdModelManagementNecessaryItemsQueryBo bo) {
        return isfdModelManagementNecessaryItemsService.queryPageList(bo);
    }

    /**
     * 获取必备件库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:query")
    @GetMapping("/{id}")
    public R<IsfdModelManagementNecessaryItemsVo> getInfo(@NotNull(message = "主键不能为空")
                                                          @PathVariable Long id) {
        return R.ok(isfdModelManagementNecessaryItemsService.queryById(id));
    }


    /**
     * 保存必备件库
     *
     * @param file 文件
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:save")
    @Log(title = "保存必备件库", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> save(@RequestParam("file") MultipartFile file,
                        @NotEmpty(message = "fullNumbers不能为空") @RequestParam("fullNumbers") String fullNumbers,
                        @RequestParam("remark") String remark) {
        try {
            return toAjax(isfdModelManagementNecessaryItemsService.save(file, fullNumbers, remark));
        } catch (Exception e) {
            log.error("必备件新增失败", e);
            throw new BaseException("必备件新增失败");
        }
    }

    /**
     * 删除必备件库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:remove")
    @Log(title = "必备件库删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody Long[] ids) {
        return toAjax(isfdModelManagementNecessaryItemsService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 删除预先查询
     *
     * @param ids 主键串
     */
    @SaCheckPermission("isfd:modelManagementNecessaryItems:pre:delete")
    @PostMapping("/pre/delete")
    public R<Boolean> preDelete(@RequestBody Long[] ids) {
        String resultStr = isfdModelManagementNecessaryItemsService.preDelete(List.of(ids));
        if (StringUtils.isNotBlank(resultStr)) {
            return R.setResult(R.SUCCESS, Boolean.FALSE, resultStr);
        }
        return R.setResult(R.SUCCESS, Boolean.TRUE, null);
    }
}
